package com.crawler.application;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;

public class Crawler {

	private JFrame frmWebCrawler;
	private JTextField textFieldURL;
	private JTextField textFieldPoliteness;
	private JTextField textFieldMaxpages;
	private JTextField textFieldURL2;
	private JComboBox<String> comboBoxMode;
	private MainWndController controller;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Crawler window = new Crawler();
					window.frmWebCrawler.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Crawler() {
		initialize();
		controller = new MainWndController();
		controller.AddMainWnd(this);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmWebCrawler = new JFrame();
		frmWebCrawler.setResizable(false);
		frmWebCrawler.setTitle("WEB Crawler");
		frmWebCrawler.setBounds(100, 100, 399, 232);
		frmWebCrawler.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmWebCrawler.getContentPane().setLayout(null);
		
		JLabel lblUrl = new JLabel("Seed URL:");
		lblUrl.setBounds(10, 11, 61, 14);
		frmWebCrawler.getContentPane().add(lblUrl);
		
		textFieldURL = new JTextField();
		textFieldURL.setBounds(10, 25, 363, 20);
		frmWebCrawler.getContentPane().add(textFieldURL);
		textFieldURL.setColumns(10);
		
		JLabel lblPoliteness = new JLabel("Politeness:");
		lblPoliteness.setBounds(10, 110, 81, 14);
		frmWebCrawler.getContentPane().add(lblPoliteness);
		
		textFieldPoliteness = new JTextField();
		textFieldPoliteness.setBounds(84, 107, 86, 20);
		frmWebCrawler.getContentPane().add(textFieldPoliteness);
		textFieldPoliteness.setColumns(10);
		
		JLabel lblMaxPages = new JLabel("Max Pages:");
		lblMaxPages.setBounds(197, 110, 69, 14);
		frmWebCrawler.getContentPane().add(lblMaxPages);
		
		textFieldMaxpages = new JTextField();
		textFieldMaxpages.setBounds(275, 107, 86, 20);
		frmWebCrawler.getContentPane().add(textFieldMaxpages);
		textFieldMaxpages.setColumns(10);
		
		JLabel lblUnrel = new JLabel("Irrelevant URL:");
		lblUnrel.setBounds(10, 56, 92, 14);
		frmWebCrawler.getContentPane().add(lblUnrel);
		
		textFieldURL2 = new JTextField();
		textFieldURL2.setBounds(10, 70, 363, 20);
		frmWebCrawler.getContentPane().add(textFieldURL2);
		textFieldURL2.setColumns(10);
		
		comboBoxMode = new JComboBox<String>();
		comboBoxMode.setBounds(197, 150, 176, 20);
		frmWebCrawler.getContentPane().add(comboBoxMode);
		//comboBoxMode.addItem("Basic Crawler");
		comboBoxMode.addItem("Generate Training Data");
		comboBoxMode.addItem("Smart Crawler");
		comboBoxMode.setSelectedIndex(0);
		
		JButton btnStart = new JButton("START");
		btnStart.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int nIndex = comboBoxMode.getSelectedIndex();
				String strURL = textFieldURL.getText();
				int nPoliteness = Integer.valueOf(textFieldPoliteness.getText());
				int nMaxPages = Integer.valueOf(textFieldMaxpages.getText());
				String strURL2 = "";
				if(nIndex == 0)
				{
					strURL2 = textFieldURL2.getText();
				}
				controller.StartCrawler(nIndex, nPoliteness, nMaxPages, strURL, strURL2);
			}
		});
		btnStart.setBounds(10, 149, 89, 23);
		frmWebCrawler.getContentPane().add(btnStart);
	}
	
	public void UpdateSettings(int nPoliteness, int nMaxPages, String strSeedURL)
	{
		textFieldPoliteness.setText(String.format("%d", nPoliteness));
		textFieldMaxpages.setText(String.format("%d", nMaxPages));
		textFieldURL.setText(strSeedURL);
	}
}
